package com.skyworth.web.admin.dishware.controller;

import com.skyworth.web.admin.dishware.domain.DishwareModel;
import com.skyworth.web.admin.dishware.form.DishwareForm;
import com.skyworth.web.admin.dishware.service.DishwareService;
import com.skyworth.web.admin.user.domain.UserModel;
import com.skyworth.web.common.Constants;
import com.skyworth.web.common.controller.BaseController;
import com.skyworth.web.common.form.MenuCss;
import com.skyworth.web.common.form.Paginator;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by skyworth on 2016/9/20.
 */

@Controller
@RequiresRoles(value={Constants.DISHWARE_ROLE})
@RequestMapping("/admin/dishware")
public class DishwareController extends BaseController {
	Logger LOG = LoggerFactory.getLogger(DishwareController.class);

	@Autowired
	private DishwareService dishwareService;

	@Autowired
	private MenuCss menuCss;


	@RequiresRoles(value={Constants.DISHWARE_ROLE})
	@RequestMapping("")
	public String index(Paginator paginator, Model model) {
		UserModel userModel = currentUser();
		long store_id = 0 ;
		if(userModel != null){
			store_id = userModel.storeId;
		}

		menuCss = new MenuCss();
		menuCss.currentFanfou = "show";
		menuCss.currentFanfouDishware = "hover";
		model.addAttribute("menuCss", menuCss);
		model.addAttribute("user", userModel);
		Page<DishwareModel> page = dishwareService.pageDishware(store_id, paginator.pageRequest());
		paginator.feedToModel(page, model);
		return "admin/dishware/dishwares";
	}

	@RequestMapping(value = {"new"})
	public String newAction(Model model) {

		menuCss = new MenuCss();
		menuCss.currentFanfou = "show";
		menuCss.currentFanfouDishware = "hover";
		model.addAttribute("menuCss", menuCss);
		model.addAttribute("user", currentUser());
		return "admin/dishware/dishware_new";
	}


	@RequestMapping("{id}")
	public String edit(@PathVariable("id") long id, Model model) {

		menuCss = new MenuCss();
		menuCss.currentFanfou = "show";
		menuCss.currentFanfouDishware = "hover";

		model.addAttribute("menuCss", menuCss);

		DishwareModel dishwareModel = dishwareService.findDishware(id);

		model.addAttribute("item", dishwareModel);
		model.addAttribute("user", currentUser());

		return "admin/dishware/dishware_edit";
	}


	@RequestMapping(value="{id}", method = RequestMethod.PUT)
	@ResponseBody
	public Map<String, Object> update(@PathVariable("id") long id,
						 @ModelAttribute DishwareForm dishwareForm,
						 Model model) {
		Map<String, Object> map = new HashMap<>();
		UserModel userModel = currentUser();
		long store_id = 0 ;
		if(userModel != null){
			store_id = userModel.storeId;
		}else{
			map.put("valid", false);
			map.put("errorMsg", "请先登陆");
			return map;
		}

		if(dishwareService.existDishware(dishwareForm.getName(), id,store_id)){
			map.put("valid", false);
			map.put("errorMsg", dishwareForm.getName()+"已存在");
			return map;
		}

		dishwareForm.setId(id);
		DishwareModel dishwareModel = dishwareService.updateDishware(dishwareForm);

		map.put("valid", true);
		map.put("url", "/admin/dishware");
		return map;
	}

	@RequestMapping(value="{id}",method = RequestMethod.DELETE)
	@ResponseBody
	public Map<String,Object> delete(@PathVariable("id") long id ) {
		DishwareModel dishwareModel = dishwareService.findDishware(id);

		Map<String, Object> rsp = responseMap();
		if (dishwareModel != null) {
			dishwareService.delDishware(id);
		}else{
			rsp.put("success", false);
			rsp.put("message", "dishware doesn't exist");
		}

		return rsp;
	}

	@RequestMapping(value = {"create"}, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> create(@ModelAttribute DishwareForm dishwareForm,
						 Model model) {
		Map<String, Object> map = new HashMap<>();
		long store_id = 0;
		UserModel userModel = currentUser();
		if(null != userModel){
			store_id = userModel.storeId;
		}else{
			map.put("valid", false);
			map.put("errorMsg", "请先登陆");
			return map;
		}

		if(dishwareService.existDishwareByName(dishwareForm.name, store_id)){
			map.put("valid", false);
			map.put("errorMsg", dishwareForm.name + "已存在");
			return map;
		}

		dishwareForm.setStoreId(store_id);
		DishwareModel dishwareModel = dishwareService.addDishware(dishwareForm);

		model.addAttribute("user", userModel);
		if (dishwareModel.getId() > 0) {
			map.put("valid", true);
			map.put("url", "/admin/dishware");
		} else {
			map.put("valid", false);
			map.put("errorMsg", "添加一次性用品失败");
		}
		return map;
	}
}